/*
 * @Author: zwz
 * @Date: 2023-06-05 10:09:22
 * @LastEditors: zwz
 * @LastEditTime: 2023-06-07 15:00:06
 * @Description: 登录接口
 */
'use strict'; // 严格模式
const { Controller } = require('egg');
// const ms = require('ms')
// const JSEncrypt = require('node-jsencrypt')

class LoginController extends Controller {
    async index() {
       const { ctx, app } = this
       const data = ctx.query;
       // 校验用户名和密码
       const userInfo = await ctx.service.user.Login(data);
       if (!userInfo) {
         ctx.failure("用户名或密码错误!",{},200432);
         return
       }
       const token = app.jwt.sign({
        username: userInfo, //需要存储的 token 数据
       }, app.config.jwt.secret);
       // 生成的token = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1NjAzNDY5MDN9.B95GqH-fdRpyZIE5g_T0l8RgzNyWOyXepkLiynWqrJg
       // 返回 token 到前端
       ctx.success('登录成功',{token: token, username:userInfo},200)
    }
}
module.exports = LoginController;